import _ from 'lodash';
import { routerRedux } from 'dva/router';
import moment from 'moment';
import * as examService from '../services/examService';

export default {
  namespace: 'exam',
  state: {
    list: [],
    pagination: {
      page: 1,
      totalPageCount: 0,
      totalCount: 0,
    },
    examLibList: [],
    examPaperList: [],
    trains: [],
    score: 0,
    correctNum: 0,
    inCorrectNum: 0,
    examList: [],
  },
  effects: {
    * fetch({ payload }, { call, put, select }) {
      let { page, filter } = payload;
      page = page || 1;
      filter = filter || {};
      const data = yield call(examService.fetch, page, 1, filter);

      yield put({ type: 'saveExam',
        payload: {
          list: data.data,
          pagination: {
            current: Number(data.meta.page),
            pageSize: Number(data.meta.pageSize),
            total: data.meta.totalCount,
          },
        },
      });
    },
    * doExam({ payload }, { call, put, select }) {
      const { shopId, branchNo, branchName, phone } = yield select(state => state.auth.currentUser);
      const params = { phone,
        selectKey: payload.selectKey,
        answer: payload.answer,
        _id: payload._id,
        ref_type: payload.ref_type,
        ref_id: payload.ref_id,
        type: payload.type,
        startTime: payload.startTime,
        shopId,
        branchNo,
        branchName,
      };
      yield call(examService.doExam, params);

      const data = yield call(examService.getTrains, phone, payload.type, payload.ref_type, payload.ref_id);
      if (data) {
        yield put({ type: 'saveTrains', payload: { trains: data && data.data ? data.data[0].trains : [] } });
        yield put({ type: 'saveScore', payload: { score: data && data.data ? data.data[0].score : 0 } });
        yield put({ type: 'saveCorrectNum', payload: { correctNum: data && data.data ? data.data[0].correctNum : 0 } });
        yield put({ type: 'saveInCorrectNum', payload: { inCorrectNum: data && data.data ? data.data[0].inCorrectNum : 0 } });
      }
    },
    * getTrains({ payload }, { call, put, select }) {
      const { phone } = yield select(state => state.auth.currentUser);
      const data = yield call(examService.getTrains, phone, payload.type, payload.ref_type, payload.ref_id);
      if (data) {
        yield put({ type: 'saveTrains', payload: { trains: data && data.data ? data.data[0].trains : [] } });
        yield put({ type: 'saveScore', payload: { score: data && data.data ? data.data[0].score : 0 } });
        yield put({ type: 'saveCorrectNum', payload: { correctNum: data && data.data ? data.data[0].correctNum : 0 } });
        yield put({ type: 'saveInCorrectNum', payload: { inCorrectNum: data && data.data ? data.data[0].inCorrectNum : 0 } });
      }
    },
    *fetchExamLibList({ payload }, { call, put, select }) {
      const data = yield call(examService.queryExamLibList, payload);
      if (data) {
        yield put({ type: 'fetchExamLibListSuccess', payload: { examLibList: data.data } });
      }
    },
    *fetchExamPaperList({ payload }, { call, put, select }) {
      const data = yield call(examService.queryExamLibList, payload);
      if (data) {
        yield put({ type: 'fetchExamPaperListSuccess', payload: { examPaperList: data.data } });
      }
    },
    *startExam({ payload }, { call, put, select }) {
      const { phone, shopId, branchNo, branchName, username, saleManName } = yield select(state => state.auth.currentUser);
      yield put({ type: 'saveTrains', payload: { trains: [] } });
      const data = yield call(examService.startExam, {
        shopId,
        phone,
        username: username || saleManName,
        branchNo,
        branchName,
        startTime: payload.startTime,
        trains: payload.trains,
        type: payload.type,
        ref_type: payload.ref_type,
        ref_id: payload.ref_id,
      });
    },
    *endExam({ payload }, { call, put, select }) {
      const { phone, shopId, branchNo } = yield select(state => state.auth.currentUser);
      yield call(examService.endExam, { shopId, phone, branchNo, type: 'exam', ref_type: payload.ref_type, ref_id: payload.ref_id });
      yield put(routerRedux.push({
        pathname: '/examReasult',
        query: {
          type: 'exam',
          time: payload.time,
          ref_type: payload.ref_type,
          ref_id: payload.ref_id,
          shopId: payload.shopId,
          examLibName: decodeURIComponent(payload.examLibName) },
      }));
    },
    * fetchExamList({ payload }, { call, put, select }) {
      const { phone, shopId } = yield select(state => state.auth.currentUser);
      payload = payload || {};
      payload.filter = { phone, shopId, ...payload.filter, type: 'exam', ref_type: 'shopExamPaper' };


      const data = yield call(examService.fetchExamList, payload);
      if (data) {
        yield put({ type: 'fetchExamListSuccess', payload: { examList: data.data } });
      }
    },
  },
  reducers: {
    saveExam(state, { payload }) {
      const { list, pagination } = payload;
      return { ...state,
        list,
        pagination: { ...state.pagination, ...pagination },
      };
    },
    saveTrains(state, { payload }) {
      return { ...state, trains: payload.trains };
    },
    saveScore(state, { payload }) {
      return { ...state, score: payload.score };
    },
    saveCorrectNum(state, { payload }) {
      return { ...state, correctNum: payload.correctNum };
    },
    saveInCorrectNum(state, { payload }) {
      return { ...state, inCorrectNum: payload.inCorrectNum };
    },
    fetchExamLibListSuccess(state, action) {
      return { ...state, ...action.payload };
    },
    fetchExamPaperListSuccess(state, action) {
      return { ...state, ...action.payload };
    },
    fetchExamListSuccess(state, { payload }) {
      return { ...state, examList: payload.examList };
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        /* if (pathname === '/examMain') {
          // dispatch({ type: 'fetch',payload: {} });
        }*/
      });
    },
  },
};
